package com.chujian.tool.controller;

import com.chujian.framework.core.base.TsDataController;
import com.chujian.framework.core.tsdata.Result;
import com.sankuai.inf.leaf.service.SegmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分布式ID生成控制器
 * 其他服务使用client调用，即可获取分布式ID
 */
@RequestMapping("/id")
@RestController
@Slf4j
public class IdGenController extends TsDataController {

    // 百度雪花算法Id生成
//    @Autowired
//    private CachedUidGenerator uidGenerator;

    @Value("${chujian.idgen.key}")
    private String idGenKey;

    // leaf 号段模式
    @Autowired
    private SegmentService segmentService;

    @RequestMapping(value = "/genId", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> genId(@RequestParam(value = "key", required = false) String key) {
        key = StringUtils.isEmpty(key) ? this.idGenKey : key;
        long id = segmentService.getId(key).getId();
        return okRs(id);
    }


}
